﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PwdMgrUI.Objects;
using PwdMgr.Utility.Exceptions;
using PwdMgr.PwdMgrService.UserAuthentication;
using PwdMgr.PwdMgrService.Sessions;
using PwdMgrUI.ViewModels;

namespace PwdMgrUI
{
    /// <summary>
    /// Interaction logic for Login.xaml
    /// </summary>
    public partial class Login : UserControl
    {
        private IAuth _authService;

        public Login()
        {
            InitializeComponent();

            this.Loaded += Login_Loaded;
        }

        void Login_Loaded(object sender, RoutedEventArgs e)
        {
            Init();
        }

        private void Init()
        {
            _authService = AuthFactory.CreateInstance();
            this.btn_Reset.Click += new RoutedEventHandler(Btn_Reset_Click);
            this.btn_Register.Click += new RoutedEventHandler(Btn_Register_Click);

            LoginViewModel model = new LoginViewModel();
            model.Username = "aaa";
            model.GetPassword = () => this.pbox_Password.Password;
            model.SetPassword = p => this.pbox_Password.Password = p;

            model.Password = "aaa";

            this.DataContext = model;
#if DEBUG
            //this.tbox_Username.Text = "a";
            //this.pbox_Password.Password = "a";
            //Btn_Login_Click(null, null);
#endif
        }

        #region event

        void Btn_Reset_Click(object sender, RoutedEventArgs e)
        {
            this.tbox_Username.Text = string.Empty;
            this.pbox_Password.Password = string.Empty;
        }

        void Btn_Login_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(this.tbox_Username.Text))
            {
                MessageBox.Show("Please input a username.",
                    "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            bool loginSuccess = false;
            Session session = null;
            try
            {
                session = _authService.Login(this.tbox_Username.Text, this.pbox_Password.Password);
                loginSuccess = true;
            }
            catch (InvalidUsernamePasswordException)
            {
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(),
                    "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            LoginEventArgs arg = new LoginEventArgs
            {
                Session = session,
                LoginSuccessfully = loginSuccess,
            };
            OnLoginCompleted(arg);
        }

        void Btn_Register_Click(object sender, RoutedEventArgs e)
        {
            RegisterWindow register = new RegisterWindow();

            register.ShowDialog();
        }

        #endregion
    }
}
